package com.wangzhixuan.commons.sqlparser;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.util.TablesNamesFinder;

import java.io.StringReader;
import java.util.List;

public class Test {
    public static  void main(String[] args) throws Exception {
        CCJSqlParserManager parserManager = new CCJSqlParserManager();

        /*String sql = "select * " +
                "from item a " +
                " left join item_sku b on a.id=b.item_id" +
                " left join erp_order c on b.sku_id=c.sku_id" +
                " where a.id in (select id from table_test) limit 0,100";*/

        String sql = "select * " +
                "from item a " +
                " left join item_sku b on a.id=b.item_id" +
                " left join (select * from table_test2) c on b.sku_id=c.sku_id" +
                " where a.id in (select id from table_test) limit 0,100";

        /*String sql = "select * " +
                "from item a " +
                " left join item_sku b on a.id=b.item_id" +
                " left join (select * from table_test2) c on b.sku_id=c.sku_id" +
                " where a.id in (select id from table_test) limit 0,100";*/

        /*String sql = "select * " +
                "from item a " +
                " left join item_sku b on a.id=b.item_id" +
                " left join (select * from table_test2) c on b.sku_id=c.sku_id" +
                " where a.id = (select id from table_test) limit 0,100";*/

        Select select = (Select)parserManager.parse(new StringReader(sql));
        //访问各个visitor
        SelectBody selectBody = select.getSelectBody();
        select.getSelectBody().accept(new SelectVisitorImpl());
        String tt = select.toString();
        System.out.println(tt);
        if(true) return;

        /*
        Statement statement = (Statement) CCJSqlParserUtil.parse(sql);
        Select selectStatement = (Select) statement;
        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
        List<String> tableList = tablesNamesFinder.getTableList(selectStatement);


        CCJSqlParserManager parserManager = new CCJSqlParserManager();
        Select select = (Select) parserManager.parse(new StringReader(sql));
        PlainSelect plain = (PlainSelect) select.getSelectBody();
        FromItem fromItem = plain.getFromItem();
        processFromItem(fromItem);



        tableList.forEach(tableName -> {
            System.out.println(tableName);
            try {
                Expression where_expression = (Expression) (CCJSqlParserUtil.parseCondExpression(tableName + ".company_id=1"));
                plain.setWhere(where_expression);

            } catch (Exception e) {

            }
        });
        System.out.println(select.toString());
        */
    }

    public static void processFromItem(FromItem fromItem) {
        if (fromItem instanceof SubJoin) {
            SubJoin subJoin = (SubJoin) fromItem;
            if (subJoin.getJoin() != null) {
                if (subJoin.getJoin().getRightItem() != null) {
                    processFromItem(subJoin.getJoin().getRightItem());
                }
            }
            if (subJoin.getLeft() != null) {
                processFromItem(subJoin.getLeft());
            }
            System.out.println(subJoin.getAlias().getName());
        }
    }
}
